const sys = require(`./system.js`)
const $ = sys.$('助学优惠卷')

// const task = 'task:common'
const task = 'task:all'
sys.run($, task, (resList, cookie, index, username, password) => {
    return new Promise(async resolve => {
        // 请求接口
        let couponsList = await $.publicJS.couponsList($, cookie)
        couponsList = couponsList.map(item => ({
          ...item,
          username,
          day: parseInt((new Date(item.expiresDate.substring(0, 10)) - new Date()) / 60 / 60 / 24 / 1000)
        }))
        // 追加信息
        resList.push(...couponsList)
        resolve(resList)
    })
}, (resList) => {
    console.log('任务结束 resList:', resList)
    if(resList && resList.length > 0) {
        // 排序
        resList = resList.sort((a1, a2) => {
            // return new Date(a1.expiresDate) - new Date(a2.expiresDate) || a2.amount - a1.amount
            return a1.day - a2.day    // 日期
            // return a2.amount - a1.amount // 金额 大到小
        })
        // 取 id
        const idList = resList.map(item => item.id)
        console.log('任务结束-存储 couponList', resList)
        // 存储
        $.localStorage.set('couponList', resList, 'localStorage')
        // $.localStorage.set(`couponList-${task}`, resList, 'localStorage')
    }
}, 3000)
